//
//  Utils.m
//  YRSideViewController
//
//  Created by ZachChen on 14-8-31.
//  Copyright (c) 2014年 YueRuo. All rights reserved.
//

#import "Utils.h"
#import "UIImage+Rotation.h"
#import <CommonCrypto/CommonCryptor.h>
#import "AppDelegate.h"

//static NSDateFormatter *dateFormatter;

@implementation Utils


+(BOOL) Dictionary:(NSDictionary*)dic ContainsNormalKey:(NSString*)key {
    if(!dic) {
        return FALSE;
    }
    id tempObj = [dic objectForKey:key];
    if (tempObj == nil) return FALSE;
    else return true;
}

+(BOOL) Dictionary:(NSDictionary*)dic ContainsArrayKey:(NSString*)key {
    if(!dic) {
        return FALSE;
    }
    id tempObj = [dic objectForKey:key];
    return [tempObj isKindOfClass:[NSArray class]];
}

+(BOOL) Dictionary:(NSDictionary*)dic ContainsDicKey:(NSString*)key {
    if(!dic) {
        return FALSE;
    }
    id tempObj = [dic objectForKey:key];
    return [tempObj isKindOfClass:[NSDictionary class]];
}

+(NSString*)StringValueForDict:(NSDictionary*)dict ForKey:(NSString*)Key {
    NSString *value;
    @try {
        value = dict[Key];
        if (!value) {
            value = @" ";
        }
    }
    @catch (NSException *exception) {
        value = @" ";
    }
    @finally {
        return value;
    }
}

+(int) phoneFontNav {
    if (SCREEN_SIZE_WIDTH < 375) {
        return 16;
    }
    return 18;
}

+(int) phoneFontNavItem {
    if (SCREEN_SIZE_WIDTH < 375) {
        return 16;
    }
    return 16;
}

+(int) phoneFontL1 {
    return 10;
}
+(int) phoneFontL2 {
    return 12;
}
+(int) phoneFontL3 {
    return 14;
}
+(int) phoneFontL4 {
    return 16;
}
+(int) phoneFontL5 {
    return 18;
}
+(int) phoneFontL6 {
    return 20;
}
+(int) phoneFontL7 {
    return 24;
}

+(int) phoneFontTime {
    if (SCREEN_SIZE_HEIGHT == 736) {
        return 12;
    }
    return 10;
}

+(int) phoneFontSmall {
    if (SCREEN_SIZE_WIDTH < 375) {
        return 12;
    }
    if (SCREEN_SIZE_HEIGHT == 736) {
        return 14;
    }
    return 12;
}

+(int) phoneFontNormal {
    if (SCREEN_SIZE_WIDTH < 375) {
        return 16;
    }
    if (SCREEN_SIZE_HEIGHT == 736) {
        return 18;
    }
    return 16;
}

+(int) phoneFontBig  {
    if (SCREEN_SIZE_HEIGHT == 736) {
        return 22;
    }
    return 20;
}

+(int) phoneFontTitle {
    if (SCREEN_SIZE_WIDTH < 375) {
        return 18;
    }
    return 18;
}

//根据不同手机高度调整比率 6为基准
// 4 5 6 6s 480 568 667 736
+(float) phoneHeightfix {
    if (SCREEN_SIZE_HEIGHT == 480) {
        return 0.6;
    }
    else if (SCREEN_SIZE_HEIGHT == 568) {
        return 0.85;
    }
    else if (SCREEN_SIZE_HEIGHT == 736) {
        return 1.1;
    }
    return 1;
}
// 4 5 6 6s 320 320 375 414
+(float) phoneWidthfix {
    if (SCREEN_SIZE_WIDTH == 320) {
        return 0.85;
    }
    else if (SCREEN_SIZE_WIDTH == 414) {
        return 1.1;
    }
    return 1;
}


+(NSDate*) NSStringToData2:(NSString*) strDate {
    NSTimeZone* localzone = [NSTimeZone localTimeZone];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    NSDate *destDate= [dateFormatter dateFromString:strDate];
    NSDate *day = [NSDate dateWithTimeInterval:(3600 + [localzone secondsFromGMT]) sinceDate:destDate];
    return day;
}
/*
+(NSDate*) NSStringToData2:(NSString*) strDate {
    NSTimeZone* localzone = [NSTimeZone localTimeZone];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    NSDate *destDate= [dateFormatter dateFromString:strDate];
    NSDate *day = [NSDate dateWithTimeInterval:(3600 + [localzone secondsFromGMT]) sinceDate:destDate];
    return day;
}*/

+ (UIImage *)getSubImage:(UIImage*) image withRect:(CGRect)rect {
    CGRect newRect = rect;
    UIImageOrientation _angleType = [image imageOrientation];
    if (UIImageOrientationDown ==_angleType) {
        image = [image imageRotatedByDegrees:180];
    }
    else if (UIImageOrientationLeft == _angleType) {
        image = [image imageRotatedByDegrees:-90];
    }
    else if (UIImageOrientationRight == _angleType) {
        image= [image imageRotatedByDegrees:90];
    }
    
    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, newRect);
    UIImage *thumbScale = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return thumbScale;
}

+(UIImage*) OriginImage:(UIImage*)image scaleToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);//size为CGSize类型，即你所需要的图片尺寸
    [image drawInRect:CGRectMake(0,0, size.width, size.height)];
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

+ (UIImage *)fixOrientation:(UIImage *)aImage {
    
    // No-op if the orientation is already correct
    if (aImage.imageOrientation == UIImageOrientationUp)
        return aImage;
    
    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
    
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
                                             CGImageGetBitsPerComponent(aImage.CGImage), 0,
                                             CGImageGetColorSpace(aImage.CGImage),
                                             CGImageGetBitmapInfo(aImage.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (aImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
            break;
    }
    
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

+ (UIImage *)cutHeadImage:(UIImage*) image Rd:(int)degree;
{
    CGFloat height = image.size.height;
    CGFloat width = image.size.width;
    if(height < width) {
        //UIImage *resImage = [image imageRotatedByDegrees:degree];
        CGFloat tmp = height;
        //height = width;
        width = tmp;
        //image = resImage;
    }
    return [Utils getSubImage:image withRect:CGRectMake(0,0, width, width)];
}

/*
一个小时以内：显示XX分钟之前
一天以内：显示XX小时之前
昨天：显示昨天
一年以内：显示具体时间，格式为 yy/mm/dd
一年以上：显示具体时间，格式为 yy/mm/dd
 */

+(NSString*) FormatNSDate:(NSDate*) Date
{
    /*if (Date.timeIntervalSinceNow > -3600) {
        return [NSString stringWithFormat:@"%d分钟前",(int)(Date.timeIntervalSinceNow/(-60))];
    }
    NSDate *now = [NSDate date];
    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *now_date = [cal components:unitFlags fromDate:now];
    NSDateComponents *cal_date = [cal components:unitFlags fromDate:Date];

    if ([now_date year] != [cal_date year] || [now_date month] != [cal_date month]) {
        return [NSString stringWithFormat:@"%d/%d/%d", (int)[cal_date year], (int)[cal_date month], (int)[cal_date day]];
    } else if ([now_date day] == [cal_date day]+1){
        return @"昨天";
    } else
        
    if ([now_date day] == [cal_date day] && [now_date month] == [cal_date month] && [now_date yea] == [cal_date day]){
        return [NSString stringWithFormat:@"%d小时前", (int)[now_date hour] - (int)[cal_date hour] + 1];
    }
    */
    return nil;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
+(void) TTAlert:(NSString*) message {
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
+(void) TTAlertNoTitle:(NSString*) message {
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

+(void) UMReport:(NSString*)key withValue:(int)value {
    NSString *numberKey = @"__ct__";
    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:@{@"name" : @"iPhone"}];
    [mutableDictionary setObject:[NSNumber numberWithInt:value] forKey:numberKey];
    [MobClick event:key attributes:mutableDictionary];
}

/******************************************************************************
 函数名称 : + (NSData *)dataWithBase64EncodedString:(NSString *)string
 函数描述 : base64格式字符串转换为文本数据
 输入参数 : (NSString *)string
 输出参数 : N/A
 返回参数 : (NSData *)
 备注信息 :
 ******************************************************************************/
+ (NSData *)dataWithBase64EncodedString:(NSString *)string
{
    if (string == nil)
        return nil;
    if ([string length] == 0)
        return [NSData data];
    
    static char *decodingTable = NULL;
    if (decodingTable == NULL)
    {
        decodingTable = malloc(256);
        if (decodingTable == NULL)
            return nil;
        memset(decodingTable, CHAR_MAX, 256);
        NSUInteger i;
        for (i = 0; i < 64; i++)
            decodingTable[(short)encodingTable[i]] = i;
    }
    
    const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
    if (characters == NULL)     //  Not an ASCII string!
        return nil;
    char *bytes = malloc((([string length] + 3) / 4) * 3);
    if (bytes == NULL)
        return nil;
    NSUInteger length = 0;
    
    NSUInteger i = 0;
    while (YES)
    {
        char buffer[4];
        short bufferLength;
        for (bufferLength = 0; bufferLength < 4; i++)
        {
            if (characters[i] == '\0')
                break;
            if (isspace(characters[i]) || characters[i] == '=')
                continue;
            buffer[bufferLength] = decodingTable[(short)characters[i]];
            if (buffer[bufferLength++] == CHAR_MAX)      //  Illegal character!
            {
                free(bytes);
                return nil;
            }
        }
        
        if (bufferLength == 0)
            break;
        if (bufferLength == 1)      //  At least two characters are needed to produce one byte!
        {
            free(bytes);
            return nil;
        }
        
        //  Decode the characters in the buffer to bytes.
        bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
        if (bufferLength > 2)
            bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
        if (bufferLength > 3)
            bytes[length++] = (buffer[2] << 6) | buffer[3];
    }
    
    bytes = realloc(bytes, length);
    return [NSData dataWithBytesNoCopy:bytes length:length];
}

/******************************************************************************
 函数名称 : + (NSString *)base64EncodedStringFrom:(NSData *)data
 函数描述 : 文本数据转换为base64格式字符串
 输入参数 : (NSData *)data
 输出参数 : N/A
 返回参数 : (NSString *)
 备注信息 :
 ******************************************************************************/
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

+ (NSString *)base64EncodedStringFrom:(NSData *)data
{
    if ([data length] == 0)
        return @"";
    
    char *characters = malloc((([data length] + 2) / 3) * 4);
    if (characters == NULL)
        return nil;
    NSUInteger length = 0;
    
    NSUInteger i = 0;
    while (i < [data length])
    {
        char buffer[3] = {0,0,0};
        short bufferLength = 0;
        while (bufferLength < 3 && i < [data length])
            buffer[bufferLength++] = ((char *)[data bytes])[i++];
        
        //  Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
        characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
        characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
        if (bufferLength > 1)
            characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
        else characters[length++] = '=';
        if (bufferLength > 2)
            characters[length++] = encodingTable[buffer[2] & 0x3F];
        else characters[length++] = '=';
    }
    
    NSString* base64String = [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];
    base64String = [base64String stringByReplacingOccurrencesOfString:@"/"
                                                           withString:@"_"];
    
    base64String = [base64String stringByReplacingOccurrencesOfString:@"+"
                                                           withString:@"-"];
    
    return base64String;
}

/******************************************************************************
 函数名称 : + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
 函数描述 : 文本数据进行DES加密
 输入参数 : (NSData *)data
 (NSString *)key
 输出参数 : N/A
 返回参数 : (NSData *)
 备注信息 : 此函数不可用于过长文本
 ******************************************************************************/
+ (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
{
    char keyPtr[kCCKeySizeAES256+1];
    bzero(keyPtr, sizeof(keyPtr));
    
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    NSUInteger dataLength = [data length];
    
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          keyPtr, kCCBlockSizeDES,
                                          NULL,
                                          [data bytes], dataLength,
                                          buffer, bufferSize,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    
    free(buffer);
    return nil;
}

/******************************************************************************
 函数名称 : + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
 函数描述 : 文本数据进行DES解密
 输入参数 : (NSData *)data
 (NSString *)key
 输出参数 : N/A
 返回参数 : (NSData *)
 备注信息 : 此函数不可用于过长文本
 ******************************************************************************/
+ (NSData *)DESDecrypt:(NSData *)data WithKey:(NSString *)key
{
    char keyPtr[kCCKeySizeAES256+1];
    bzero(keyPtr, sizeof(keyPtr));
    
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    NSUInteger dataLength = [data length];
    
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          keyPtr, kCCBlockSizeDES,
                                          NULL,
                                          [data bytes], dataLength,
                                          buffer, bufferSize,
                                          &numBytesDecrypted);
    
    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }
    
    free(buffer);
    return nil;
}

+ (NSString *)base64StringFromText:(NSString *)text
{
    if (text) {
        //取项目的bundleIdentifier作为KEY
        NSString *key = DESKey;
        NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
        //IOS 自带DES加密 Begin
        data = [self DESEncrypt:data WithKey:key];
        //IOS 自带DES加密 End
        NSString *base64String = [self base64EncodedStringFrom:data];
        
        return base64String;
    }
    else {
        return nil;
    }
}

+ (NSString *)textFromBase64String:(NSString *)base64
{
    if (base64) {
        //取项目的bundleIdentifier作为KEY
        NSString *key = DESKey;
        NSData *data = [self dataWithBase64EncodedString:base64];
        //IOS 自带DES解密 Begin
        data = [self DESDecrypt:data WithKey:key];
        //IOS 自带DES加密 End
        return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
    else {
        return nil;
    }
}

+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize
{
    UIImage *newimage;
    if (nil == image) {
        newimage = nil;
    }else{
        CGSize oldsize = image.size;
        CGRect rect;
        if (asize.width/asize.height > oldsize.width/oldsize.height) {
            rect.size.width = asize.height*oldsize.width/oldsize.height;
            rect.size.height = asize.height;
            rect.origin.x = (asize.width - rect.size.width)/2;
            rect.origin.y = 0;
        }else{
            rect.size.width = asize.width;
            rect.size.height = asize.width*oldsize.height/oldsize.width;
            rect.origin.x = 0;
            rect.origin.y = (asize.height - rect.size.height)/2;
        }
        UIGraphicsBeginImageContext(asize);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
        [image drawInRect:rect];
        newimage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    return newimage;
}

+ (NSString *)HXChatterToUMDBUser:(NSString *)hxChatter {
    NSString* dbuser = hxChatter;
    if ([hxChatter hasPrefix:@"user_"]) {
        dbuser = [hxChatter substringFromIndex:5];
    }
    return dbuser;
}

+ (NSString *)UIDToHXChatter:(int)UID {
    if (UID < 10000) {
        return [NSString stringWithFormat:@"%d",UID];
    } else {
        return [NSString stringWithFormat:@"user_%d",UID];
    }
}

+ (NSDictionary*) JsonParse:(NSString*) JsonData
{
    //NSString *test = @"{\"ts\" : 1431693627520,\"avatars\" : [{\"avatarId\" : 1,\"url\" : \"http://7u2pgh.com1.z0.glb.clouddn.com/2.pic_hd.jpg\"}, {\"avatarId\" : 2,\"url\" : \"http://7u2pgh.com1.z0.glb.clouddn.com/157_5.jpg\"} ]}";
    //NSString *test = @"{\"ts\" : 1431693627520,\"avatars\" : [ 1,2,3 ]}";
    //NSString *test = @"{\"a\":[1,2,3]}";
    if (JsonData == nil)
        return nil;
    NSError *error;
    NSDictionary *jsonres = [NSJSONSerialization JSONObjectWithData:[JsonData dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];
    //NSDictionary *jsonres = [NSJSONSerialization JSONObjectWithData:[JsonData dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
    return jsonres;
}

+(UIButton*) NavButtonStyleNormalBLeft:(BOOL)bLeft NormalImg:(NSString*)Image AndHightLight:(NSString*) ImageH {
    int padding = 0;
    if (bLeft) padding = -20;
    UIImage* IImage = [UIImage imageNamed:Image];
    UIImage* IImageH = [UIImage imageNamed:ImageH];
    UIButton * Button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30.0f, 30.0f)];
    //[backButton setTitle:@"返回" forState:UIControlStateNormal];
    //backButton.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    //[Button setTitleEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
    [Button setImageEdgeInsets:UIEdgeInsetsMake(0, padding, 0, 0)];
    [Button setImage:IImage forState:UIControlStateNormal];
    [Button setImage:IImageH forState:UIControlStateHighlighted];
    return Button;
}
/*
极寒 －40℃或低于此值
奇寒 －35～－39.9℃
酷寒 －30～－34.9℃
严寒 －20～－29.9℃
深寒 －15～－19.9℃
大寒 －10～－14.9℃
小寒 －5～－9.9℃
轻寒 －4.9～0℃
微寒 0～4.9℃
凉 5～9.9℃
温凉 10～11.9℃
微温凉 12～13.9℃
温和 14～15.9℃
微温和 16～17.9℃
温暖 18～19.9℃
暖　20～21.9℃
热 22～24.9℃
炎热 25～27.9℃
暑热 28～29.9℃
酷热 30～34.9℃
奇热 35～39℃
极热 高于40℃*/
+ (NSString *)getTemptureDes:(float) deg {
    NSArray *arr = @[@"极寒",@"奇寒",@"酷寒",@"严寒",@"深寒",@"大寒",@"小寒",
                     @"轻寒",@"微寒",@"凉",@"温凉",@"微温凉",@"温和",@"温暖",
                     @"暖", @"热", @"炎热", @"暑热", @"酷热", @"奇热", @"极热"];
    int a[20] = {-40,-35,-30,-20,-15,-5,-0,5,10,12,14,16,18,20,22,25,28,30,35,40};
    int i;
    for (i=0;i<20;i++) {
        if (deg < a[i]) break;
    }
    return [NSString stringWithFormat:@"%.1f°(%@)", deg,arr[i]];
}

+ (NSString *)getWetDes:(float) deg {
    NSArray *arr = @[@"干燥",@"适宜",@"潮湿"];
    int a[2] = {30,80};
    int i;
    for (i=0;i<2;i++) {
        if (deg < a[i]) break;
    }
    return [NSString stringWithFormat:@"%d%%(%@)", (int)deg,arr[i]];
}

+ (NSString *)getPMDes:(int) deg {
    NSArray *arr = @[@"优",@"良",@"轻度污染",@"中度污染",@"重度污染",@"严重污染"];
    int a[5] = {35,75,115,150,250};
    int i;
    for (i=0;i<5;i++) {
        if (deg < a[i]) break;
    }
    return [NSString stringWithFormat:@"%d(%@)", deg,arr[i]];
}


@end

